package cn.hejiang.mamis.core.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import cn.hejiang.mamis.core.dto.OptionDTO;
import cn.hejiang.mamis.core.dto.ResultDTO;
import cn.hejiang.mamis.core.entity.SessionDetails;
import cn.hejiang.mamis.core.entity.SysDept;
import cn.hejiang.mamis.core.service.ContextService;
import cn.hejiang.mamis.core.service.PlatSysService;
import cn.hejiang.mamis.core.service.SysDeptService;
import cn.hejiang.mamis.core.util.EntityUtil;
import cn.hejiang.mamis.core.util.QueryUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

@RequestMapping("/mamis/sys/dept")
@RestController
@Slf4j
public class SysDeptController {
	@Autowired
	ContextService contextService;

	@Autowired
	SysDeptService sysDeptService;

	@Autowired
	PlatSysService platSysService;

	@GetMapping("/option/list")
	public ResultDTO<Object> getDeptOptionList(final HttpServletRequest request) {
		String curSysId = contextService.getSystem().getId();

		// 查询数据库
		final List<SysDept> deptList = sysDeptService.getDeptList(curSysId);
		final List<SysDept> deptTree = EntityUtil.plainToTree(deptList);
		final List<OptionDTO> deptOptionTree = EntityUtil.toOptionTree(deptTree);

		List<Map<String, Object>> rootList = new ArrayList<>();

		// 虚拟名为系统名称的根节点
		Map<String, Object> root = new HashMap<String, Object>();
		rootList.add(root);

		root.put("label", platSysService.getSysById(curSysId).getName());
		root.put("value", "root");
		root.put("children", deptOptionTree);
		root.put("creatable", true);
		root.put("editable", false);
		root.put("removable", false);

		// 返回结果集
		return ResultDTO.success(rootList);
	}

	@PostMapping("/option")
	public ResultDTO saveDeptOption(final HttpServletRequest request, @RequestBody OptionDTO obj) {
		final SessionDetails session = contextService.getSession(request);

		SysDept entity = new SysDept();

		// 提取数据信息
		entity.setName(obj.getLabel());
		if (obj.getParent() != null) {
			String value = obj.getParent().getValue();
			if (StrUtil.equals(value, "root")) {
				value = null;
			}
			entity.setPid(value);
		}

		// 设置更新人员
		entity.setUpdatedBy(session.getCurUserId());

		boolean ret;
		if (StrUtil.isNotBlank(obj.getValue())) {
			// 更新

			// 设置更新条件
			entity.setId(obj.getValue());

			ret = sysDeptService.updateById(entity);
		} else {
			// 插入

			// 设置创建人员
			entity.setCreatedBy(session.getCurUserId());

			// 设置所属关系
			entity.setOwnDeptId(session.getCurDeptId());
			entity.setOwnSysId(contextService.getSystem().getId());

			ret = sysDeptService.save(entity);
		}

		// 返回结果集
		return ret ? ResultDTO.success() : ResultDTO.error("创建部门失败");
	}

	@DeleteMapping("/option/{id}")
	public ResultDTO delDeptOption(final HttpServletRequest request, @PathVariable("id") String id) {
		UpdateWrapper<SysDept> wrapper = QueryUtil.newDeleteWrapper(id);
		boolean ret = sysDeptService.update(null, wrapper);

		return ret ? ResultDTO.success() : ResultDTO.error("删除失败");
	}

}
